<?php

class Util {
    
    public static function getController(){
        $ArURL = array(); 
        if(isset($_GET['url']))
            $ArURL = explode("/", $_GET["url"]);
        return !empty($ArURL[0]) ? $ArURL[0] : 'Viagem';
    }
    
    public static function getAction(){
        $ArURL = array();
        if(isset($_GET['url']))
            $ArURL = explode("/", $_GET["url"]);
        return !empty($ArURL[1]) ? $ArURL[1] : 'index';
    }
    
    public static function getParameters(){
        $ArURL = explode("/", $_GET["url"]);
        $ArParameters = array();
        for($i = 2; $i < count($ArURL); $i++){
            $ArParameters[$ArURL[$i++]] = isset($ArURL[$i]) ? $ArURL[$i] : '';
        }
        return $ArParameters;
    }
    
    public static function returnPage(){
        header("Location: " . $_SERVER['HTTP_REFERER']);
    }
    
    public static function redirectTo($url){
        header("location: " . BASEDIR . $url);
    }
    
    public static function formatTimestamp($timestamp){
        return date("d/m/Y, H:i:s", strtotime($timestamp));
    }
    
    public static function getData($timestamp){
        return date("d/m/Y", strtotime($timestamp));
    }
    
    public static function getHora($timestamp){
        return date("H:i:s", strtotime($timestamp));
    }
    
    public static function formatMoney($money){
        return number_format($money, 2, ',', '');
    }
    
    public static function dateToSQL($date){
        $ArDate = explode('/', $date);
        if(count($ArDate) < 3)
            return null;
        return $ArDate[2] . '-' . $ArDate[1] . '-' . $ArDate[0];
    }
    
    public static function createHTMLSelect($ArObject, $selectName, $valueColumn, $textColumn, $selectedValue = ''){
        $Select = "<select name='{$selectName}'>\n";
        $Select .= "\t<option value=''>[Selecione]</option>\n";
        $valueColumn = 'get' . $valueColumn;
        $textColumn = 'get' . $textColumn;
        foreach($ArObject as $Object){
            $Select .= "\t<option value='{$Object->$valueColumn()}' ";
            $Select .= $Object->$valueColumn() == $selectedValue ? 'selected' : '';
            $Select .= ">{$Object->$textColumn()}</option>\n";
        }
        $Select .= "</select>\n";
        return $Select;
    }
    
    function validaCPF($cpf = null) {

        // Verifica se um número foi informado
        if(empty($cpf)) {
            return false;
        }

        // Elimina possivel mascara
        $cpf = preg_replace('[^0-9]', '', $cpf);
        $cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);

        // Verifica se o numero de digitos informados é igual a 11 
        if (strlen($cpf) != 11) {
            return false;
        }
        // Verifica se nenhuma das sequências invalidas abaixo 
        // foi digitada. Caso afirmativo, retorna falso
        else if ($cpf == '00000000000' || 
            $cpf == '11111111111' || 
            $cpf == '22222222222' || 
            $cpf == '33333333333' || 
            $cpf == '44444444444' || 
            $cpf == '55555555555' || 
            $cpf == '66666666666' || 
            $cpf == '77777777777' || 
            $cpf == '88888888888' || 
            $cpf == '99999999999') {
            return false;
        // Calcula os digitos verificadores para verificar se o
        // CPF é válido
        } else {   

            for ($t = 9; $t < 11; $t++) {

                for ($d = 0, $c = 0; $c < $t; $c++) {
                    $d += $cpf{$c} * (($t + 1) - $c);
                }
                $d = ((10 * $d) % 11) % 10;
                if ($cpf{$c} != $d) {
                    return false;
                }
            }

            return true;
        }
    }
    
    public static function validaData($data){
        return preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $data);
    }
    
    public static function validaCEP($cep){
        return preg_match('/^\d{8}$/', $cep);
    }
    
    public static function validaTelefone($tel){
        return preg_match('/^\(\d{2}\)\d{8,9}$/', $tel);
    }
    
    public static function validaEmail($email){
        return filter_var($email, FILTER_VALIDATE_EMAIL);
    }
}

?>
